<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\UserType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\UserType AS UserTypeModel;
use App\Models\User AS UserModel;
use App\Admin\Actions\User\Restore;
use App\Admin\Actions\User\BatchRestore;
use App\Models\GoodsCollection;
class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(['userType']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('nickname')->editable();
            $grid->column('city',"注册地")->sortable();
//            $grid->column('phone');
            $grid->column('l_m')->display(function ($l_m){
                switch ($l_m){
                    case 0:
                       return 'm';
                       break;
                    case 1:
                        return "l";
                        break;
                    default:
                        return "m";
                }
            });
            $grid->column('come_from')->display(function ($come_from){
                return UserModel::$comeFromMap[$come_from];
            });
            $grid->column('userType.name',"用户类别");
            $grid->column('has_wx')->display(function ($has_wx){
                switch ($has_wx){
                    case 0:
                        return "无";
                        break;
                    case 1:
                        return "有";
                        break;
                    default:
                        return "无";
                }
            });
            $grid->column('deal_time');
            $grid->column('is_seller')->bool();
            $grid->column('log_time')->sortable();
            $grid->column("sin_in_day")->sortable();


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like("nickname");
                $filter->like("phone");
                $filter->equal('city',"注册判断地");
                $filter->equal("come_from")->select(
                  UserModel::$comeFromMap
                );
                $filter->equal("cate_type")->select(
                  UserType::all()->pluck('name','id')
                  ->toArray()
                );
                $filter->between('created_at', "注册时间")->datetime();
                $filter->equal("is_seller","是否是销售")
                    ->select([
                        1=>"是",
                        0=>"否"
                    ]);
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(UserModel::class));
                }
                $count = GoodsCollection::where("user_id",$actions->row->id)->count();
                $url = <<<URL
/cpgdeal/collection_goods?user_id={$actions->row->id}
URL;
                $change_integral = <<<URL
/cpgdeal/user_integral_logs/create?user_id={$actions->row->id}
URL;

                $user_watch_logs = <<<URL
/cpgdeal/goods_watch_logs?user_id={$actions->row->id}
URL;

//dd($url);
                $actions->append('<a href="'.$user_watch_logs.'"><i class="fa fa-eye"></i>浏览记录</a>');
                $actions->append('<a href="'.$url.'"><i class="fa fa-heart"></i>收藏列表('.$count.')</a>');
                $actions->append('<a href="'.$change_integral.'"><i class="fa fa-scribd"></i>积分变动</a>');
            });
            // 也可以这样
            $grid->disableBatchDelete();
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(UserModel::class));
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('email_verified_at');
            $show->field('password');
            $show->field('remember_token');
            $show->field('phone');
            $show->field('phone_s');
            $show->field('phone_t');
            $show->field('log_time');
            $show->field('nickname');
            $show->field('l_m');
            $show->field('come_from');
            $show->field('cate_type');
            $show->field('no_short_msg');
            $show->field('birthday_year');
            $show->field('birthday_md');
            $show->field('address');
            $show->field('address_detail');
            $show->field('has_wx');
            $show->field('car_type');
            $show->field('hobby');
            $show->field('location_id');
            $show->field('mark');
            $show->field('location_address');
            $show->field('sex');
            $show->field('deal_time');
            $show->field('unionid');
            $show->field('web_openid');
            $show->field('mini_openid');
            $show->field('wx_mini_session_key');
            $show->field('wx_web_session_key');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->tab("基本信息",function (Form $form){
                $form->display('id');
                $form->text('nickname')->required();
                $form->mobile('phone');
                $form->switch("is_seller");
                $form->select('sex')->options(
                    UserModel::$sexMap
                )->default(3);
                $form->select('cate_type')->options(
                    UserTypeModel::all()->pluck("name","id")
                        ->toArray()
                );
//                $form->switch('no_short_msg')->default(0);
            })->tab("详细信息",function (Form $form){
                $form->switch('has_wx');
                $form->switch('l_m')->default(0);
                $form->text('deal_time');
                $form->date('birthday')
                    ->value($form->model()->birthday_year."-".$form->model()->birthday_md);
                $form->text('address');
                $form->text('address_detail');
                $form->text('car_type');
                $form->text('hobby');
//                $form->text('location_id');
                $form->text('location_address');
                $form->text('mark');
                $form->hidden("birthday_year");
                $form->hidden("birthday_md");
                $form->hidden("come_from")->value(UserModel::COME_FROM_ADMIN);
            });

            $form->saving(function (Form $form) {
                if ($form->input("birthday")){
                    $birthday = explode("-",$form->input("birthday"));
                    $form->birthday_year = $birthday[0];
                    $form->birthday_md = $birthday[1]."-".$birthday[2];
                }
                $form->deleteInput('birthday');
//                dd($form);
            });
        });
    }
}
